#include "thumbnail_process.h"
#include "image_constants.h"
#include "auxiliaryfunction.h"
#include <QImage>
#include <QByteArray>
#include <QRect>
#include <QtEndian>
//test
#include <QDebug>


ThumbnailProcess::ThumbnailProcess()
{
    image_all_line_ = qFromBigEndian(image_all_line_);
    image_start_line_ = qFromBigEndian(image_start_line_);
    image_end_line_ = qFromBigEndian(image_end_line_);
    image_per_line_bytes_ = qFromBigEndian(image_per_line_bytes_);

    image_prefix_message_.append(AuxiliaryFunction::ShortToArray(image_all_line_));
    image_prefix_message_.append(AuxiliaryFunction::ShortToArray(image_start_line_));
    image_prefix_message_.append(AuxiliaryFunction::ShortToArray(image_end_line_));
    image_prefix_message_.append(AuxiliaryFunction::ShortToArray(image_per_line_bytes_));
}

ThumbnailProcess::~ThumbnailProcess()
{

}

int ThumbnailProcess::setROI(unsigned short x, unsigned short y, unsigned short width, unsigned short height)
{
    if(x + width > IMAGE_WIDTCH || y + height > IMAGE_HEIGHT){
        return IMAGE_ERR;
    }
    image_x_ = x;
    image_y_ = y;
    if(width == IMAGE_WIDTCH || width == limit_slice_width_){
        image_width_ = width;
    }
    if(height == IMAGE_HEIGHT || height == limit_slice_height){
        image_height_ = height;
    }
    return IMAGE_OK;
}

int ThumbnailProcess::getROI(unsigned short &x, unsigned short &y, unsigned short &width, unsigned short &height) const
{
    x = image_x_;
    y = image_y_;
    width = image_width_;
    height = image_height_;
    return IMAGE_OK;
}

int ThumbnailProcess::generateUploadImage(const unsigned char *image_pointer)
{
    //判断开窗是否超出范围
    if(image_x_ > IMAGE_WIDTCH || image_y_ > IMAGE_HEIGHT || image_x_ + image_width_ > IMAGE_WIDTCH || image_y_ + image_height_ >IMAGE_HEIGHT){
        return OUT_OF_RANGE;
    }

    if(image_x_ == 0 && image_y_ == 0 && image_width_ == IMAGE_WIDTCH && image_height_ == IMAGE_HEIGHT){
        generateThumbnail(image_pointer);
    }else{
        generateSlice(image_pointer);
    }
    ++image_count_number_;

    emit sigGenerateUploadImageSuccess();

    return IMAGE_OK;
}

int ThumbnailProcess::getUploadImagedata(QByteArray &upload_image)
{
    upload_image = upload_bytearray_;

    return IMAGE_OK;
}

int ThumbnailProcess::generateThumbnail(const unsigned char *const image_pointer)
{
    upload_bytearray_.clear();
    unsigned int image_count = qFromBigEndian(image_count_number_);
    upload_bytearray_.append(AuxiliaryFunction::IntToArray(image_count));
    upload_bytearray_.append(image_prefix_message_);

    unsigned int image_per_ten_line_bytes = IMAGE_PER_LINE_BYTES * 10;
    for(unsigned int i = 0; i < 600; i++){
        unsigned int line_num = image_per_ten_line_bytes * i;
        for(int j = 1; j <= 800; j++){
            upload_bytearray_.append(*(image_pointer+j*20-1+line_num));
        }
    }
    return IMAGE_OK;
}

int ThumbnailProcess::generateSlice(const unsigned char * const image_pointer)
{
    if(image_width_ > limit_slice_width_ || image_height_ > limit_slice_height){
        return OUT_OF_RANGE;
    }

    QRect temp_rect = QRect(image_x_, image_y_, limit_slice_width_, limit_slice_height);
    QImage image_cut = QImage(image_pointer,IMAGE_WIDTCH,IMAGE_HEIGHT,QImage::Format_Grayscale16);
    image_cut = image_cut.copy(temp_rect);
    const unsigned char* image_data = image_cut.bits();
    QByteArray image_byte;
    for(int i = 1;i <= limit_slice_width_*limit_slice_height;i++){
        image_byte.append(*(image_data+i*2-1));
    }
    upload_bytearray_.clear();
    upload_bytearray_ = image_byte;

    return IMAGE_OK;
}
